<?php

namespace app\api\server;

use app\common\library\Auth;
use app\common\model\Circulation;
use app\common\model\GoldLog;
use app\common\model\goods\Goods;
use app\common\model\User as ModelUser;
use app\common\model\UserGroup;
use think\Controller;
use think\Db;
use think\Exception;

/**
 * 用户逻辑
 */
class User extends Controller
{

    /**
     * 绑定用户
     *
     */
    public static function binding_relationship($params)
    {
        extract($params);
        Db::startTrans();
        try {
            $user = ModelUser::get($uid);
            if($user->pid>0){
                return true;
                Db::commit();
            }
            $pUser = ModelUser::where('qr_code', $qr_code)->where('id', '<>', $uid)->find();
            $check = ModelUser::where("find_in_set(:id,parent_ids)", ['id' => $uid])->where('qr_code', $qr_code)->count();
            if ($check) {
                exception('邀请码错误');
            }
            if (!$pUser) {
                exception('邀请人不存在');
            }
            $pUser->setInc('first_num', 1);
            if ($pUser->pid) {
                $tUser = ModelUser::get($pUser->pid);
                if ($tUser) {
                    $user->tid =  $pUser->pid;
                    $tUser->setInc('two_num', 1);
                }
            }
            //绑定关系
            $user->parent_ids =  trim($pUser->id . ',' . $pUser['parent_ids'], ',');
            $user->pid =  $pUser->id;
            $user->is_first = 0;
            $user->save();
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            exception($e->getMessage());
        }
        return true;
    }


    //共享金兑换
    public static function exchangegold($data)
    {
        extract($data);
        if ($num <= 0) {
            exception('请输入兑换数量');
        }
        if (!isset($type) || !$type) {
            exception('类型错误');
        }
        $user = ModelUser::get($user_id);
        if ($type == 1) {
            $gold = $user->tg_gold;
        } else {
            $gold = $user->xf_gold;
        }
        if ($num > $gold) {
            exception('推广金不足');
        }
        try {
            $circulation = Circulation::whereTime('date', 'today')->value('circulation');
            $conversion_ratio = config('site.conversion_ratio');
            $conversion = $conversion_ratio / 100;
            $money = bcmul($num * $circulation, $conversion, 2);
            ModelUser::gold($type, -$num, $user_id, $msg, $circulation, $money);
            ModelUser::money($money, $user_id, '共享金兑换余额');
            // ModelUser::foun($money, $user_id, '共享金兑换');
        } catch (\Exception $e) {
            exception($e->getMessage());
        }
    }
    //检测用户是否满足升级条件
    public static function upgrade($user)
    {
        $group_id = $user->group_id;
        $level = $group_id++;
        //下一级升级条件
        $group = UserGroup::get($level);
        switch ($group_id) {
            case 2:
                if ($user->first_num >= $group->first_num && $user->two_num >= $group->two_num) {
                    $user->setInc('group_id', 1);
                }
                break;
            case 3:
                $count = ModelUser::where("find_in_set({$user->id},parent_ids)")->where('group_id', '>', 2)->count();
                if ($count++ >= $group->team_num) {
                    $user->setInc('group_id', 1);
                }
                break;
            case 4:
                //董事人数
                $count = ModelUser::where("find_in_set({$user->id},parent_ids)")->where('group_id', '>', 3)->count();
                //团队人数
                $num = ModelUser::where("find_in_set({$user->id},parent_ids)")->count();
                if ($count >= $group->manager_num && $num >= $group->team_size) {
                    return true;
                }
                break;
        }
        return false;
    }

    //查询下级所有平级用户
    static function diguis($uid, $group_id)
    {
        $ids = [];
        $users = ModelUser::where('pid', $uid)->field('id,group_id')->select();
        if ($users) {
            foreach ($users as $k => $v) {
                if ($v['group_id'] >= $group_id) {
                    array_push($ids, $v['group_id']);
                } else {
                    self::diguis($v, $group_id);
                }
            }
        }
        return $ids;
    }

    //查询下级所有用户
    static function recursion($uid, &$arr)
    {

        $users = ModelUser::where('pid', $uid)->column('id');
        if ($users) {
            foreach ($users as $k => $v) {
                if ($v['group_id'] < $users['group_id']) {
                    array_push($arr, $v['id']);
                    self::recursion($v['id'], $arr);
                }
            }
        }
    }
}
